package cn.zlp.org.controller;

import cn.zlp.basic.util.AjaxResult;
import cn.zlp.basic.util.PageList;
import cn.zlp.org.domain.Department;
import cn.zlp.org.query.DepartmentQuery;
import cn.zlp.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制接口
 * Api注解:接口类的api描述
 * @author Mr.Zhang
 */
@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /** 查询所有
     * 定义一个方法：返回值：List<Department> +  方法名：findAll + 参数列表：无
     * 处理请求：Get http://localhost:8080/department
     * GetMapping等价于RequestMapping(value = "",method = RequestMethod.GET)
     * ApiOperation(value = "查询所有接口",notes = ""):value为接口方法的描述,notes为参数的提示
     */
    @GetMapping
    @ApiOperation(value = "查询所有接口",notes = "")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /** 查询单个
     * 以前处理请求：Get http://localhost:8080/department?id=1
     * 现在处理请求：Get http://localhost:8080/department/1
     * GetMapping("/{id}")用{}区分是请求参数，没有用{}是请求地址
     * PathVariable用来接收传过来的参数id
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个接口",notes = "需要传入参数id")
    public Department findOne(@PathVariable("id")Long id){
        return departmentService.findById(id);
    }

    /** 分页查询 */
    @PostMapping
    @ApiOperation(value = "分页查询接口",notes = "需要传入分页参数")
    public PageList<Department> pageList(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.pageQuery(departmentQuery);
    }

    /** 删除 */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口",notes = "需要传入参数id")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("删除失败");
        }
    }

    /** 批量删除 */
    @PatchMapping
    @ApiOperation(value = "批量删除接口",notes = "需要传入参数id集合")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("删除失败");
        }
    }

    /** 修改或者添加 */
    @PutMapping
    @ApiOperation(value = "修改或新增接口",notes = "需要传入部门参数,如果有id就是修改否则就是添加")
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        try {
            // 没有id说明为新增,调用add方法
            if (department.getId() == null){
                departmentService.add(department);
            } else { // 有id说明为修改,调用update方法
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("操作失败");
        }
    }

    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门树",notes = "不需要传入参数")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

}
